﻿/**
* QSmart Andon
*
* 2012-10-23
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.BasicDataManage
{
    public class LevelService : DbHelperBase
    {
        public int GetLevelCount(LevelQcEntity qcmodel)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_Level AS A WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.ETypeCode))
            {
                strSql.Append(" AND ETypeCode ='" + qcmodel.ETypeCode + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" AND LineID ='" + qcmodel.LineID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.TimeoutStatus))
            {
                strSql.Append(" AND TimeoutStatus =" + qcmodel.TimeoutStatus);
            }
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        public List<LevelEntity> GetLevelList(LevelQcEntity qcmodel)
        {
            var list = new List<LevelEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by A.LevelIdx,A.InputTime) AS idx,A.[LvID],C.LineID,C.LineName,A.[ETypeCode],B.ETypeName,A.[LevelIdx],A.[MsgTimeOut],A.[TimeoutStatus],A.[InputTime] FROM [UT_Level] AS A LEFT JOIN UT_EType AS B ON A.ETypeCode=B.ETypeCode LEFT JOIN UT_Line AS C ON A.LineID=C.LineID ");
            strSql.Append(" WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.ETypeCode))
            {
                strSql.Append(" AND A.ETypeCode ='" + qcmodel.ETypeCode + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" AND A.LineID ='" + qcmodel.LineID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.TimeoutStatus))
            {
                strSql.Append(" AND A.TimeoutStatus =" + qcmodel.TimeoutStatus);
            }
            strSql.Append(" ) AS T ");

            strSql.Append(" Where T.idx between " + start + " and " + end);

            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new LevelEntity();
                            model.LvID = ReaderHelper.GetValue<Guid>(reader, "LvID");
                            model.LineID = ReaderHelper.GetValue<Guid>(reader, "LineID");
                            model.LineName = ReaderHelper.GetValue<string>(reader, "LineName");
                            model.ETypeCode = ReaderHelper.GetValue<string>(reader, "ETypeCode");
                            model.ETypeName = ReaderHelper.GetValue<string>(reader, "ETypeName");
                            model.LevelIdx = ReaderHelper.GetValue<int>(reader, "LevelIdx");
                            model.MsgTimeOut = ReaderHelper.GetValue<int>(reader, "MsgTimeOut");
                            model.TimeoutStatus = ReaderHelper.GetValue<int>(reader, "TimeoutStatus");
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public LevelEntity GetLevelInfo(string lvId)
        {
            var model = new LevelEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT A.[LvID],A.LineID,A.[ETypeCode],B.ETypeName,A.[LevelIdx],A.[MsgTimeOut],A.[TimeoutStatus],A.[InputTime] FROM [UT_Level] AS A LEFT JOIN UT_EType AS B ON A.ETypeCode=B.ETypeCode where A.[LvID]=@LvID ");
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                Db.AddInParameter(cmd, "@LvID", lvId);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            model.LvID = ReaderHelper.GetValue<Guid>(reader, "LvID");
                            model.LineID = ReaderHelper.GetValue<Guid>(reader, "LineID");
                            model.ETypeCode = ReaderHelper.GetValue<string>(reader, "ETypeCode");
                            model.ETypeName = ReaderHelper.GetValue<string>(reader, "ETypeName");
                            model.LevelIdx = ReaderHelper.GetValue<int>(reader, "LevelIdx");
                            model.MsgTimeOut = ReaderHelper.GetValue<int>(reader, "MsgTimeOut");
                            model.TimeoutStatus = ReaderHelper.GetValue<int>(reader, "TimeoutStatus");
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }

        public int GetMaxLevelIdx(LevelQcEntity qcmodel)
        {
            var strSql = new StringBuilder();
            int result = 0;
            strSql.Append(" SELECT MAX(LevelIdx) AS LevelIdxCount FROM UT_Level WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.ETypeCode))
            {
                strSql.Append(" AND ETypeCode ='" + qcmodel.ETypeCode + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" AND LineID ='" + qcmodel.LineID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.TimeoutStatus))
            {
                strSql.Append(" AND TimeoutStatus =" + qcmodel.TimeoutStatus);
            }
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            result = Convert.ToInt32(reader["LevelIdxCount"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return result;
            }
        }

        public bool AddLevel(LevelEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO [UT_Level]([LvID],[LineID],[ETypeCode],[LevelIdx],[MsgTimeOut],[TimeoutStatus]) ");
            strSql.Append(" VALUES(@LvID,@LineID,@ETypeCode,@LevelIdx,@MsgTimeOut,@TimeoutStatus) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LvID", model.LvID);
                    Db.AddInParameter(cmd, "@LineID", model.LineID);
                    Db.AddInParameter(cmd, "@ETypeCode", model.ETypeCode);
                    Db.AddInParameter(cmd, "@LevelIdx", model.LevelIdx);
                    Db.AddInParameter(cmd, "@MsgTimeOut", model.MsgTimeOut);
                    Db.AddInParameter(cmd, "@TimeoutStatus", model.TimeoutStatus);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool ModifyLevel(LevelEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_Level SET [MsgTimeOut] = @MsgTimeOut,[TimeoutStatus]=@TimeoutStatus WHERE [LvID] = @LvID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@MsgTimeOut", model.MsgTimeOut);
                    Db.AddInParameter(cmd, "@TimeoutStatus", model.TimeoutStatus);
                    Db.AddInParameter(cmd, "@LvID", model.LvID);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool RemoveLevel(string id)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_Level Where LvID=@LvID");
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LvID", id);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
    }
}
